/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mongoosehome.web.admin.dao;

import com.mongoosehome.web.common.domain.TModule;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Administrator
 */
public class ModuleDaoImpl extends HibernateDaoSupport implements ModuleDao{
    
    public List<TModule> getModulesByParent(TModule parent) {
        List<Integer> roleId = new ArrayList<Integer>();
        roleId.add(1);
        if(parent == null){
            return (List<TModule>)getHibernateTemplate().find("select m from TModule as m inner join m.roles r where m.parent is null order by m.moduleId");
        }else{
            return (List<TModule>)getHibernateTemplate().find("select m from TModule as m inner join m.roles r where m.parent=? order by m.moduleId", new Object[]{parent});
       }
    }
    
    public List<TModule> getModuleByRoleIds(List<Integer> roleIds){
        StringBuilder hql = new StringBuilder("select m from TModule as m inner join m.roles r where 1 = 1");
        if(roleIds != null && roleIds.size() > 0){
            hql.append(" and  r.roleId in(");
            for(int i =0;i <  roleIds.size(); i++){
                if(i == (roleIds.size() -1)){
                    hql.append(roleIds.get(i));
                }else{
                    hql.append(roleIds.get(i)).append(",");
                }
            }
            hql.append(")");
        }
        hql.append(" order by m.moduleId");
        return (List<TModule>)getHibernateTemplate().find(hql.toString());
    }
}
